package PIPL::Class::SingletonTest;


package PIPL::Class::SingletonTest::Package1;

use base 'PIPL::Class::Singleton';

1;


package PIPL::Class::SingletonTest;

use strict;

use base 'Test::Unit::TestCase';

our $Initial_Package1_Self = __PACKAGE__ . $$;  # random unique value

sub new {
    my $self = shift()->SUPER::new(@_);
    return $self;
}

sub set_up {
    # save initial value
    $Initial_Package1_Self = $PIPL::Class::SingletonTest::Package1::Self;
}

sub test_PIPL_Class_Singleton_new {
    my $self = shift;
    my $obj1 = PIPL::Class::SingletonTest::Package1->new;
    $self->assert_not_null($obj1);
    $self->assert($obj1->isa('PIPL::Class::Singleton'));
    $self->assert($obj1->isa('PIPL::Class::SingletonTest::Package1'));
    my $obj2 = $obj1->new;
    $self->assert_not_null($obj2);
    $self->assert($obj2->isa('PIPL::Class::Singleton'));
    $self->assert($obj2->isa('PIPL::Class::SingletonTest::Package1'));
    $self->assert_not_equals($obj1, $obj2);
}

sub test_PIPL_Class_Singleton_instance {
    my $self = shift;
    $self->assert_null($Initial_Package1_Self);
    my $obj1 = PIPL::Class::SingletonTest::Package1->instance;
    my $obj2 = PIPL::Class::SingletonTest::Package1->instance;
    $self->assert_not_null($obj1);
    $self->assert_not_null($obj2);
    $self->assert($obj1->isa('PIPL::Class::Singleton'));
    $self->assert($obj1->isa('PIPL::Class::SingletonTest::Package1'));
    $self->assert($obj2->isa('PIPL::Class::Singleton'));
    $self->assert($obj2->isa('PIPL::Class::SingletonTest::Package1'));
    $self->assert_equals($obj1, $obj2);
}

sub test_PIPL_Class_Singleton_Self {
    my $self = shift;

    my $obj = PIPL::Class::SingletonTest::Package1->instance;
    $self->assert_not_null($obj);
    $self->assert($obj->isa('PIPL::Class::Singleton'));
    $self->assert($obj->isa('PIPL::Class::SingletonTest::Package1'));

    $self->assert_not_null($PIPL::Class::SingletonTest::Package1::Self);
    $self->assert($PIPL::Class::SingletonTest::Package1::Self->isa('PIPL::Class::Singleton'));
    $self->assert($PIPL::Class::SingletonTest::Package1::Self->isa('PIPL::Class::SingletonTest::Package1'));
    $self->assert_equals($obj, $PIPL::Class::SingletonTest::Package1::Self);
}

sub test_PIPL_Class_Singleton_destroy {
    my $self = shift;

    my $obj = PIPL::Class::SingletonTest::Package1->instance;
    $self->assert_not_null($obj);
    $self->assert($obj->isa('PIPL::Class::Singleton'));
    $self->assert($obj->isa('PIPL::Class::SingletonTest::Package1'));

    PIPL::Class::SingletonTest::Package1->destroy;
    $self->assert_null($PIPL::Class::SingletonTest::Package1::Self);
}

1;
